package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallRole;
import org.linlinjava.litemall.db.mapper.LitemallRoleDao;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;

@Service
public class LitemallRoleService {
    @Resource
    private LitemallRoleDao roleMapper;


    public Set<String> queryByIds(Integer[] roleIds) {
        Set<String> roles = new HashSet<String>();
        if(roleIds.length == 0){
            return roles;
        }
        
        LambdaQuery<LitemallRole> query = roleMapper.createLambdaQuery();
        
        query.andIn(LitemallRole::getId, Arrays.asList(roleIds));
        query.andEq(LitemallRole::getEnabled, true);
        query.andEq(LitemallRole::getDeleted, false);
        List<LitemallRole> roleList  =query.select(LitemallRole::getName);
		/*
		 * LitemallRoleExample example = new LitemallRoleExample();
		 * example.or().andIdIn(Arrays.asList(roleIds)).andEnabledEqualTo(true).
		 * andDeletedEqualTo(false); List<LitemallRole> roleList =
		 * roleMapper.selectByExample(example);
		 */

        for(LitemallRole role : roleList){
            roles.add(role.getName());
        }

        return roles;

    }

    public List<LitemallRole> querySelective(String name, Integer page, Integer limit, String sort, String order) {
    	
    	LambdaQuery<LitemallRole> query = roleMapper.createLambdaQuery();
    	if (!StringUtils.isEmpty(name)) {
    		query.andLike(LitemallRole::getName, "%" + name + "%");
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	query.orderBy(sort + " " + order);
        }
        query.andEq(LitemallRole::getDeleted, false);
        query.limit(page, limit);
        return query.select();
    	
		/*
		 * LitemallRoleExample example = new LitemallRoleExample();
		 * LitemallRoleExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (!StringUtils.isEmpty(name)) { criteria.andNameLike("%" + name + "%"); }
		 * criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 

        PageHelper.startPage(page, limit);
        return roleMapper.selectByExample(example);
        */
    }

    public LitemallRole findById(Integer id) {
    	return roleMapper.single(id);
        //return roleMapper.selectByPrimaryKey(id);
    }

    public void add(LitemallRole role) {
        role.setAddTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insertTemplate(role,true);
        //roleMapper.insertSelective(role);
    }

    public void deleteById(Integer id) {
    	
    	LitemallRole role = new LitemallRole();
    	role.setId(id);
    	role.setDeleted(true);
    	roleMapper.updateTemplateById(role);
        //roleMapper.logicalDeleteByPrimaryKey(id);
    }

    public void updateById(LitemallRole role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.updateTemplateById(role);
        //roleMapper.updateByPrimaryKeySelective(role);
    }

    public boolean checkExist(String name) {
    	
    	LitemallRole role = new LitemallRole();
    	role.setName(name);
    	role.setDeleted(false);
    	return roleMapper.templateCount(role)!=0;
		/*
		 * LitemallRoleExample example = new LitemallRoleExample();
		 * example.or().andNameEqualTo(name).andDeletedEqualTo(false); return
		 * roleMapper.countByExample(example) != 0;
		 */
    }

    public List<LitemallRole> queryAll() {
    	LitemallRole role = new LitemallRole();
    	role.setDeleted(false);
    	return roleMapper.template(role);
		/*
		 * LitemallRoleExample example = new LitemallRoleExample();
		 * example.or().andDeletedEqualTo(false); return
		 * roleMapper.selectByExample(example);
		 */
    }
}
